#include "fenetre.h"
#include <QMenu>
#include <QMenuBar>
#include <QMessageBox>
#include <QEvent>
#include <QScrollBar>
#include <string>
#include <iostream>
#include <sstream>

using namespace std;

Fenetre::Fenetre(QWidget * parent, Qt::WindowFlags flags)
    : QMainWindow(parent,flags)
{

    user = new QtChatClient();
    ////////////////////////////////////////////////////////////
    QAction *new_server_action = new QAction("&New Server",this);
    QAction *connect_server_action = new QAction("&Connect To Server",this);
    QAction *disconnect_server_action = new QAction("&Disconnect",this);
    QAction *quit_action = new QAction("&Quit",this);

    new_server_action->setToolTip("Create a new server");

    QMenu * menuAction= menuBar()->addMenu("&Action");

    menuAction->addAction(new_server_action);
    menuAction->addAction(connect_server_action);
    menuAction->addAction(disconnect_server_action);
    menuAction->addSeparator();
    menuAction->addAction(quit_action);

    connect(new_server_action,        SIGNAL(triggered()),this, SLOT(createNewServer()));
    connect(connect_server_action,    SIGNAL(triggered()),this, SLOT(connectToServer()));
    connect(disconnect_server_action, SIGNAL(triggered()),this, SLOT(disconnectServer()));
    connect(quit_action,              SIGNAL(triggered()),this, SLOT(close()));

    ////////////////////////////////////////////////////////////

    messageText = new QLineEdit(this);
    affichageText= new QTextEdit(this);
    affichageText->setReadOnly(true);

    connect(messageText, SIGNAL(returnPressed()),this, SLOT(commandeReceiver()));
    connect(messageText, SIGNAL(editingFinished()),this, SLOT(resetMessageText()));

    connect(user, SIGNAL(newStringMsg(const QString &)),this,SLOT(getMessage(const QString &)));
    connect(user, SIGNAL(information(const QString &)),this,SLOT(getInfo(const QString &)));


    QVBoxLayout * layout = new QVBoxLayout;

    //verticalLayout->addItem();
    //this->setLayoutDirection(Qt::Vertical);//new QVBoxLayout());
    layout->addWidget(affichageText);
    layout->addWidget(messageText);

    this->setCentralWidget(new QWidget);
    this->centralWidget()->setLayout(layout);
}

void Fenetre::createNewServer()
{
    QMessageBox::warning(this,QString("Action clicked"),QString("CreateNewServer()"));
}

void Fenetre::connectToServer() {
    QMessageBox::warning(this,QString("Action clicked"),QString("connectToServer()"));
}

void Fenetre::disconnectServer() {
    QMessageBox::warning(this,QString("Action clicked"),QString("disconnectServer()"));
}

void Fenetre::commandeReceiver(){

    if( messageText->text().size() < 1 ) {
        return;
    }
    else if(messageText->text()[0] == '!' ) {
        istringstream command(messageText->text().toStdString());
        string nameCommand;

        command >> nameCommand;

        if( nameCommand == "!pseudo") {
            string pseudo;
            command >> pseudo;
            user->setPseudonyme(QString("%1").arg(pseudo.c_str()));
        }
        else if(nameCommand == "!connect") {
            string server,port;
            command >> server;
            command >> port;

            user->connectServer(server.c_str(),port.c_str());
        }
        else if(nameCommand == "!bye") {
            user->disconnectServer();
        }
        else {
            getInfo(QString("Unknown command : %1").arg(messageText->text()));
        }
    }
    else {
        user->sendStringMsg(messageText->text());
    }
}

void Fenetre::resetMessageText(){
    messageText->setText("");
}

void Fenetre::getMessage(const QString &a_msg){
    affichageText->insertPlainText(QString("%1\n").arg(a_msg));
    QScrollBar * scrollBar = this->affichageText->verticalScrollBar();
    scrollBar->setValue(scrollBar->maximum());
}

void Fenetre::getInfo(const QString &a_msg){
    affichageText->insertPlainText(QString("Info : %1\n").arg(a_msg));
    QScrollBar * scrollBar = this->affichageText->verticalScrollBar();
    scrollBar->setValue(scrollBar->maximum());
}
